package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayPermission;
import org.itboys.mobile.entity.pos.product.PosProduct;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.service.mongo.web.SundayPermissionService;
import org.itboys.mobile.service.pos.PosProductService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_权限_controller_web
 *
 */
@Controller
@RequestMapping("/sunday/web/permission")
public class SundayPermissionController extends BaseController {

    @Autowired
    private SundayPermissionService permissionService;
    @Autowired
    private PosProductService posProductService;
    /**
     * 用户  主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                         @RequestParam(value="memberId",required=true) Long memberId,
                         @RequestParam(value="memberNumber",required=true) String memberNumber,
                         Model model){
        model.addAttribute("memberId",memberId);
        model.addAttribute("memberNumber",memberNumber);
        return "/sunday/permission/index";
    }

    /**
     * 分页门店商品库存信息
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="memberId",required=true) Long memberId
    ) {
        try {
            PageResult<SundayPermission> result=permissionService.selectPermission(request,memberId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 同步门店价格和库存
     * @param request
     * @param response
     * @param shopNumber

     */
    @RequestMapping("/processStoreAndPriceToApp")
    public  void processStoreAndPriceToApp(HttpServletRequest request,
                      HttpServletResponse response,
                      @RequestParam(value="shopNumber",required=true) String shopNumber
    ) {
        try {
            posProductService.processStoreAndPriceToApp(shopNumber);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/permission/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }


}
